package com.luoshenshen.socker;


import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author ShenshenLuo
 * @date 2025/4/19 17:36
 **/
public class MyWebSockerHandler extends SimpleChannelInboundHandler<TextWebSocketFrame> {


    @Override
    protected void channelRead0(ChannelHandlerContext ctx, TextWebSocketFrame msg) throws Exception {

        System.out.printf("server received: %s\n", msg.text());

        ctx.writeAndFlush(new TextWebSocketFrame("server received: " + msg.text()));
        ctx.channel().writeAndFlush(new TextWebSocketFrame("server received: " + msg.text()) + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
    }

    @Override
    public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {

        ctx.writeAndFlush(new TextWebSocketFrame("server received: "));
        ctx.channel().writeAndFlush(new TextWebSocketFrame("server received: "+ new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())));
    }

    @Override
    public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
        System.out.printf("channel active : %s\n", ctx.channel().id().asLongText());
    }

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        System.out.printf("channel active : %s\n", ctx.channel().id().asLongText());
    }

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {

        System.out.printf("channel active : %s\n", ctx.channel().id().asLongText());
    }

    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
        System.out.printf("channel active : %s\n", ctx.channel().id().asLongText());
    }
}
